package features.advance.jdk.dynamicproxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author LIN
 * @date 2021-04-26
 */
public class JdkProxy implements InvocationHandler {

    private TargetClass targetClass;

    public Object createProxy(TargetClass targetClass){

        this.targetClass = targetClass;
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class<?>[] interfaces = targetClass.getClass().getInterfaces();
        System.out.println(interfaces.length);
        return Proxy.newProxyInstance(classLoader,interfaces,this);

    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDK前置增强");
        Object invoke = method.invoke(targetClass, args);
        System.out.println("JDK后置增强");
        return invoke;
    }
}
